package org.sky.sys.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.Logger;
import org.sky.exception.ServiceException;
import org.sky.sys.entity.SysQuartz;
import org.sky.sys.mapper.SysQuartzMapper;
import org.sky.sys.mapper.SysCommonMapper;
import org.sky.sys.util.BspUtils;
import org.sky.sys.util.page.PageListData;
import org.sky.sys.util.page.PageListDataUtils;
import org.sky.util.LogUtils;
import org.sky.util.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
* @author weifx
* @Title: SysQuartzService
* @ProjectName platform-core
* @Description: TODO{计划任务业务类}
* @date 2019-3-14 21:50:03
* @Version 1.0
*/
@Service
public class SysQuartzService {
    private final static Logger log = LogUtils.getLogger(SysQuartzService.class);
    @Autowired
    private SysQuartzMapper sysQuartzMapper;
    @Autowired
    private SysCommonMapper sysCommonMapper;
    /**
    * 计划任务分页查询
    * @param pageNum
    * @param pageSize
    * @param example
    * @return
    */
    public PageListData selectSysQuartzByPage(int pageNum, int pageSize, Example example){
        PageHelper.startPage(pageNum, pageSize);
        final PageInfo<SysQuartz> pageInfo = new PageInfo<SysQuartz>(sysQuartzMapper.selectByExample(example));
        return PageListDataUtils.getPageListData().setTotal(pageInfo.getTotal()).setRows(pageInfo.getList());
    }
    /**
    * 保存{新增/修改}计划任务
    * @param sysQuartz
    * @throws ServiceException
    */
    public void saveAddEditSysQuartz(SysQuartz sysQuartz) throws ServiceException{
        try {
            Timestamp ts = sysCommonMapper.queryTimestamp();
            if (null == sysQuartz.getId()) {
                Long id = SnowflakeIdWorker.getInstance().nextId();
                sysQuartz.setId(id);
                sysQuartz.setCreater(BspUtils.getLoginUser().getCode());
                sysQuartz.setCreateTime(ts);
                sysQuartz.setUpdater(BspUtils.getLoginUser().getCode());
                sysQuartz.setUpdateTime(ts);
                sysQuartzMapper.insertSelective(sysQuartz);
            } else {
                sysQuartz.setUpdater(BspUtils.getLoginUser().getCode());
                sysQuartz.setUpdateTime(ts);
                Example e = new Example(SysQuartz.class);
                e.createCriteria().andEqualTo("id", sysQuartz.getId());
                sysQuartzMapper.updateByExampleSelective(sysQuartz,e);
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("计划任务保存失败:{}",e.getMessage());
            throw new ServiceException("计划任务保存失败:"+e.getMessage(),e);
        }
    }
    /**
    * 根据ID删除计划任务
    * @param list
    */
    public void delSysQuartzById(List<SysQuartz> list){
        try {
            List idList = new ArrayList();
            for (SysQuartz sysQuartz : list) {
                idList.add(sysQuartz.getId());
            }
            Example example = new Example(SysQuartz.class);
            example.createCriteria().andIn("id", idList);
            sysQuartzMapper.deleteByExample(example);
        }catch (Exception e){
            e.printStackTrace();
            log.error("计划任务删除失败:{}",e.getMessage());
            throw new ServiceException("计划任务删除失败:"+e.getMessage(),e);
        }
    }
    /**
    * 根据Id查询计划任务
    * @param id
    * @return
    */
    public SysQuartz getSysQuartzById(Long id){
        Example example = new Example(SysQuartz.class);
        example.createCriteria().andEqualTo("id",id);
        return sysQuartzMapper.selectOneByExample(example);
    }
}
